from django.shortcuts import render
from django.http import HttpResponse
from app_update.models import AppUpdate
from django.views.decorators.csrf import csrf_exempt
import json
from django.core import serializers
from datetime import datetime


@csrf_exempt
def app_update_view(request):
    if request.method == 'POST':
        obj = AppUpdate.objects.latest('release_time')

        if obj is not None:
            release_time = obj.release_time
            release_time = release_time.strftime("%Y-%m-%d")

            response_data = dict()
            response_data['status'] = 'success'
            data = dict()
            data['app_update_version'] = obj.app_update_version
            data['app_update_intro'] = obj.app_update_intro
            data['app_update_detail'] = obj.app_update_detail
            data['app_update_url'] = obj.app_update_url
            data['app_update_size'] = obj.app_update_size
            data['release_time'] = release_time
            response_data['data'] = data
            response_data['message'] = 'niui bi !, you got it!'

            json_data = json.dumps(response_data)

            return HttpResponse(json_data)
        else:
            response_data = dict()
            response_data['status'] = 'error'
            response_data['message'] = 'data not found in server'
    else:
        return HttpResponse('method not support.')
